<?php require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
  MYDCONN();
  $GLOBALS['MYT'] = $GLOBALS['MYD'];
  $TOT = 0;
	$Search = 'WHERE ';
  	if (isset($_POST['TimeType']) && $_POST['TimeType'] != '') {
	$TimeTime = $_POST['TimeType'];
	} else {
	$TimeTime = "TimePay";
	}
   if (isset($_POST['Search'])) {
  	if (isset($_POST['IReference']) && $_POST['IReference'] != '') {
	$Search .= "ProvIReference = '".$_POST['IReference']."' AND ";
	}
  	if (isset($_POST['Invoice']) && $_POST['Invoice'] != '') {
	$Search .= "ccenter_invoices.Ref = '".$_POST['Invoice']."' AND ";
	}
  	if (isset($_POST['Provider']) && $_POST['Provider'] != '') {
	$Search .= "RefProvider = '".$_POST['Provider']."' AND ";
	}
  	if (isset($_POST['Amount']) && $_POST['Amount'] != '') {
	$Search .= "Amount = '".$_POST['Amount']."' AND ";
	}
  	if (isset($_POST['Conta']) && $_POST['Conta'] != '') {
	$Search .= "CODE LIKE '".$_POST['Conta']."%' AND ";
	}
}

  	if (isset($_POST['TimeEnd']) && $_POST['TimeEnd'] != '') {
		if ($TimeTime == 'TimeClose') {
	$Search .= "{$TimeTime} BETWEEN '".$_POST['TimeStart']." 00:00:00' AND '".$_POST['TimeEnd']." 23:59:59' AND ";

		} else {
	$Search .= "({$TimeTime} BETWEEN '".$_POST['TimeStart']." 00:00:00' AND '".$_POST['TimeEnd']." 23:59:59' OR {$TimeTime} = '0000-00-00 00:00:00') AND ";
		}
	}
  	if (isset($_POST['Status']) && $_POST['Status'] != '') {
	$Search .= "ccenter_invoices.Status = ".$_POST['Status'];
	} else {
	$Search .= "ccenter_invoices.Status != 5";
	}
    $GET = $GLOBALS['MYD']->db_array("SELECT
					ccenter_invoices.Ref AS COLID,
					CONCAT(ccenter_invoices.Ref,'/',YEAR(ccenter_invoices.Time)) AS COL0,
					ccenter_invoices.ProvIReference AS COL1,
					ccenter_invoices.Description AS COL2,
					ccenter_providers.name AS COL3,
					ccenter_invoices.CODE AS COL4,
					ccenter_invoices.Amount AS COL5,
					DATE(ccenter_invoices.{$TimeTime}) AS COL6,
					hcce_status.name AS COL7,
					hcce_paytypes.name AS COL8
				FROM ccenter_invoices
				INNER JOIN hcce_status ON hcce_status.StatRef = ccenter_invoices.Status AND hcce_status.Type = 1
				LEFT OUTER JOIN hcce_paytypes ON hcce_paytypes.Ref = ccenter_invoices.TYPEPay
				LEFT OUTER JOIN main_offices ON main_offices.ref = ccenter_invoices.RefOffice
				LEFT OUTER JOIN main_users ON main_users.ref = ccenter_invoices.RefUser
				LEFT OUTER JOIN ccenter_providers ON ccenter_providers.Ref = ccenter_invoices.RefProvider
				{$Search} 
				ORDER BY ccenter_invoices.Ref ASC");
	$CARD = $GET[0]; ?>
<div id="poolResults"></div>
<div id="gridResults"></div>
<script charset="utf-8" type="text/javascript">

 var  windowHeight = document.documentElement.clientHeight-200;
Ext.onReady(function(){
    Ext.QuickTips.init();
    var myData = [
	<?php
	for ($i=1;$i<=$CARD;$i++) {
		$TOT += $GET[$i]['COL5'];
	echo '['.$i.','.$GET[$i]['COLID'].',
	"'.$GET[$i]['COL0'].'",
	"'.trimall($GET[$i]['COL1']).'",
	"'.trimall($GET[$i]['COL2']).'",
	"'.trimall(str_replace('"',"'",$GET[$i]['COL3'])).'",
	"'.$GET[$i]['COL4'].'",
	"'.$GET[$i]['COL5'].'",
	"'.$GET[$i]['COL6'].'",
	"'.$GET[$i]['COL7'].'",
	"'.$GET[$i]['COL8'].'"]';
	if ($i<$CARD) {
			echo ',';
		}
	 } ?>
    ];

    var ds = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
        {name: 'id'},
		{name: 'REF', type: 'int'},
		{name: 'O_ID', type: 'string'},
        {name: 'COL1', type: 'string'},
        {name: 'COL2', type: 'string'},
        {name: 'COL3', type: 'string'},
		{name: 'COL4', type: 'string'},
        {name: 'COL5', type: 'string'},
        {name: 'COL6', type: 'string'},
        {name: 'COL7', type: 'string'},
        {name: 'COL8', type: 'string'}
        ])
    });
    ds.loadData(myData);
    var colModel = new Ext.grid.ColumnModel([
        {header: "Referencia", width: 60, sortable: true, locked:true, dataIndex: 'REF'},
        {header: "N.Interno", width: 80, sortable: true, locked:true, dataIndex: 'O_ID'},
        {header: "N.Factura", width: 80, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Observacoes", width: 300, sortable: true, dataIndex: 'COL2'},
        {header: "Fornecedor", width: 160, sortable: true, locked:true,dataIndex: 'COL3'},
        {header: "Natureza", width: 80, sortable: true, align: 'right', locked:true,dataIndex: 'COL4'},
        {header: "Valor", width: 120, sortable: true, align: 'right', locked:true,dataIndex: 'COL5'},
        {header: "Data", width: 80, sortable: true, locked:true,dataIndex: 'COL6'},
        {header: "Estado", width: 100, sortable: true, locked:true,dataIndex: 'COL7'},
        {header: "Pagamento", width: 100, sortable: true, locked:true,dataIndex: 'COL8'}
    ]);

	Bars = new Ext.Toolbar({
			height: 25,
				items : [{
				type:'button',
	            text:'# <?php echo $GET[0];?> | Valor: <?php echo number_format($TOT,2).' '.$__sys['CurrencyISO'];?>'
			},{
				type:'button',
				loadMask: true,
                text:'Pagar Seleccionados',
                handler:function(){
					var ss = ''; 
					var m = selmode.getSelections();				
					for(var i = 0, len = m.length; i < len; i++){
								ss += m[i].get("REF") + ",";}
							document.getInvoices.POOL.value = ss;
							document.getInvoices.action='_response/rsproxy.php?pr=cdc_bulkpay';
							subform('getInvoices','poolResults');
							}
					}]
		});

   var selmode = new Ext.grid.RowSelectionModel({});
   var Console = new Ext.grid.GridPanel({
        ds: ds,
        height: 500,
	bbar: Bars,
	renderTo: 'gridResults',
        cm: colModel,
        sm: selmode,
		listeners: {
	 		rowdblclick: function(ds, row) { 
			var record = ds.getStore().getAt(row);
			var value = record.get('REF');	
			idwindow('Propriedades de Factura','winccfac','','Modules/CentrodeCustos/Lib/addinvoice.php?CCID='+value,980,600);
			}
		}
    });
});
</script>